package com.hc.service.impl;

import com.hc.bean.User;
import com.hc.dao.UserDao;
import com.hc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserServiceImpl1 implements UserService {
//    @Autowired
    @Resource
    private UserDao userDao;

    //从spring容器中获取RedisTemplate实例
//    @Autowired
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<User> findAll() {
        List<User> userList=userDao.findAll();
        String key="users:userList";
        ValueOperations valueOperations=redisTemplate.opsForValue();
        valueOperations.set(key,userList);
        return userList;
    }

    @Override
    public List<User> findUserByName(String name) {
        return userDao.findUserByName(name);
    }

    @Override
    public Integer addUser(User user) {
        int result=userDao.addUser(user);
        if(result>0){
            /*如果添加成功,更新缓存中的数据*/
            redisTemplate.delete("users:userList");
        }
        return result;
    }

    @Override
    public Integer updateUser(User user) {
        int result = userDao.updateUser(user);

        if (result > 0) {
            String key = "users:user:" + user.getId();
            ValueOperations valueOperations = redisTemplate.opsForValue();
            //修改实体数据库的同时需要修改redis中的数据
            //方式1：直接删除key
            redisTemplate.delete(key);
            redisTemplate.delete("users:userList");
            //方式2：更新redis中的数据
//            valueOperations.set(key, user);
        }
        return result;
    }

    @Override
    public Integer deleteUser(Integer uid) {
        int result=userDao.deleteUser(uid);
        if(result>0){
            /*如果删除成功,更新缓存中的数据*/
            redisTemplate.delete("users:userList");
        }
        return result;
    }

    @Override
    public User getUserById(Integer uid) {
        User user = null;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = "users:user:" + uid;
        if (uid != null) {
            //判断key是否存在，如果存在则从redis中取出数据，如果不存在直接从数据库中查
            if (redisTemplate.hasKey(key)) {
                Object obj = valueOperations.get(key);
                if (obj != null) {
                    user = (User) obj;
                }
                System.out.println("从缓存中获取");
            } else {
                //从数据库中查询，然后把结果再放入redis中
                user = userDao.getUserById(uid);
                if (user != null) {
                    valueOperations.set(key, user);
                    //设置过期时间
                    redisTemplate.expire(key,60*30, TimeUnit.SECONDS);
                }
                System.out.println("从数据库中获取");
            }
        }
        return user;
    }
}
